<?php

$thisPage = "blprofile";
$subpage = getRequestParameter("subpage", false);

TemplateHelper::useLayout("form");

$params = unserialize(stripslashes(getrequestparameter("params")));

addDataService("getlogo");
addHtmlService("getlogo");
addDataservice("save");
addHtmlService("view");
addDataservice("view");

function buildLayout(&$smarty) {
    global $subpage;

    if ($subpage === false) {
        if (Auth::userHasProfile()) {
            $smarty->assign("form", getProfileFormData($smarty));
        } else {
            $smarty->assign("form", getNoProfileFormdata($smarty));
        }
    } else if ($subpage == "success") {
        $smarty->assign("form", getServiceProfileSuccessData($smarty));
    } else if ($subpage == "view") {
        $smarty->assign("form", getViewData($smarty));
    } else if ($subpage == "saverequest") {
        $smarty->assign("form", getSaveRequestFormData($smarty));
    }
}

function executeService($serviceName, $params, $smarty) {
    switch ($serviceName) {
        case "getlogo" : { return service_getcompanylogo($params); }
        case "save" : { return service_save($params); }
        case "view" : { return service_view($params, $smarty); }
    }
}

function service_getcompanylogo($params) {
    Database::loadModel("files");
    $data = Database::getItem($params['fileid']);
    return asImage($data['type'], $data['content']);
}

function service_view($params, $smarty) {
    $viewdata = getViewData($smarty);
    return $viewdata['content'];
}

function service_save($params) {
    Database::loadModel("companies");
    if (Auth::userHasProfile()) {
        $params['company_id'] = Auth::getProfileId();
        $profileId = Auth::getProfileId();
        Database::getModel()->removeHS4($profileId);
/*
        Database::getModel()->setBusinessLead($profileId,
            $params['productname'],
            $params['description'],
            $params['buysell'],
            $params['hs4code'],
            $params['quantity'],
            $params['unit'],
            $params['country_origin'],
            $params['country_dest']
        );
*/
    } else {
        $profileId = Database::addNew($params);
        Auth::setCompanyProfileId($profileId);
        Database::loadModel("users");
        Database::getModel()->setCompanyProfileId($profileId);
/*
        Database::getModel()->setBusinessLead($profileId,
            $params['productname'],
            $params['description'],
            $params['buysell'],
            $params['hs4code'],
            $params['quantity'],
            $params['unit'],
            $params['country_origin'],
            $params['country_dest']
        );
*/
    }
    Database::loadModel("businessleads");
    $id = Database::addNew($params);
    Database::assignTo("company", $profileId, $id);
    return serviceSuccessfulWithRedirect(array(), "blprofile", "success");
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter();
}

function getProfileFormData(&$smarty) {
    global $params;
    global $thisPage;
    Log::debug("Executing function: getProfileFormData");
    if (Auth::isLoggedIn()) {
        initDatabase();
        Database::loadModel("companies");
        $companyProfile = Database::getItem(Auth::getProfileId());
        $smarty->assign("blprofile", array(
            posted => htmlspecialchars($params['text'])
        ));
        $smarty->assign("companyprofile", $companyProfile);
        return array(
            title => "Post your business leads",
            content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "blprofile_content"))
        );
    } else {
        return TemplateHelper::redirectPage("login", "", array(
            redirecturl => "index.php?choice=blprofile&params=" . urlencode(serialize(array(
                text => htmlspecialchars($params['text']),
            )))
        ));
    }
}

function getSaveRequestFormData(&$smarty) {
    global $thisPage;
    global $params;

    if (Auth::isLoggedIn()) {
        return array(
            title => "Saving request to immediate store",
            content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "blprofile_saverequest"))
        );
    } else {
        return TemplateHelper::redirectPage("login", "", array(
            redirecturl => "index.php?choice=blprofile&subpage=saverequest" . urlencode(serialize(array(
                request => $params['request'],
                type => $params['type'],
            )))
        ));
    }
}

function getNoProfileFormdata(&$smarty) {
    global $thisPage;
    return array(
        title => "Company profile does not exist",
        content => $smarty->fetch(TemplateHelper::getTemplatefile($thisPage, "blprofile_noprofile")),
    );
}

function getServiceProfileSuccessData(&$smarty) {
    global $thisPage;
    return array(
        title => "Post your business leads",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "blprofile_success"))
    );
}

function getViewData(&$smarty) {
    global $thisPage;
    global $params;

    if (Auth::isLoggedIn()) {
        initDatabase();
        Database::loadModel("companies");
        $item = Database::getItem($params['company_id']);
        $bldata = array();
        if ($params['displayMode'] == "preview") {
            $bldata = Database::getModel()->getBusinessLead($params['company_id'], true);
        } else {
            $bldata = Database::getModel()->getBusinessLead($params['company_id'], false);
        }
        $smarty->assign("data", array_merge($item, $bldata));
        return array(
            title => "Business leads profile",
            content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "blprofile_view"))
        );
    } else {
        return TemplateHelper::redirectPage("login", "", array(
            redirecturl => "index.php?choice=blprofile&subpage=view&params=" . urlencode(serialize(array(
                company_id => $params['company_id']
            )))
        ));
    }
}

?>
